package com.study.zkyxgs1.controller;

import com.study.zkyxgs1.model.User;
import com.study.zkyxgs1.service.UserService;
import com.study.zkyxgs1.utils.ResultMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 人员管理
 * @author weixiaochun
 * @date 2022/6/14 20:34
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation("添加人员信息")
    @PostMapping("/insert")
    public ResultMap insert(@RequestBody User user){
        userService.insert(user);
        return ResultMap.ok();
    }

    @ApiOperation("根据id更新人员信息")
    @PutMapping("/update")
    public ResultMap update(@RequestBody User user){
        return userService.update(user) ? ResultMap.ok() : ResultMap.error("更新失败，请稍后再试");
    }

    @ApiOperation("根据id删除人员信息")
    @DeleteMapping("/delete/{id}")
    public ResultMap delete(@PathVariable("id") String id){
        return userService.delete(id) ? ResultMap.ok() : ResultMap.error("删除失败，请稍后再试");
    }

    @ApiOperation("根据id查询人员信息")
    @GetMapping("/selectById/{id}")
    public ResultMap selectById(@PathVariable("id") String id){
        User user = userService.selectById(id);
        return ResultMap.ok().data("user", user);
    }

    @ApiOperation("根据姓名查询人员信息")
    @GetMapping("/selectByName/{name}")
    public ResultMap selectByName(@PathVariable("name") String name){
        List<User> users = userService.selectByName(name);
        return ResultMap.ok().data("users", users);
    }

    @ApiOperation("根据年龄查询人员信息")
    @GetMapping("/selectByAge/{age}")
    public ResultMap selectByAge(@PathVariable("age") Integer age){
        List<User> users = userService.selectByAge(age);
        return ResultMap.ok().data("users", users);
    }

    @ApiOperation("根据爱好集合查询人员信息")
    @GetMapping("/selectByHobby")
    public ResultMap selectByHobby(@RequestParam List<String> hobby){
        List<User> users = userService.selectByHobby(hobby);
        return ResultMap.ok().data("users", users);
    }

    @ApiOperation("查询所有人员信息")
    @GetMapping("/selectAll")
    public ResultMap selectAll(){
        List<User> users = userService.selectAll();
        return ResultMap.ok().data("users", users);
    }

    @ApiOperation("根据年龄范围查询人员信息")
    @GetMapping("/selectByAgeRange/{startAge}/{endAge}")
    public ResultMap selectByAgeRange(@PathVariable("startAge") Integer startAge,
                                    @PathVariable("endAge") Integer endAge){
        List<User> users = userService.selectByAgeRange(startAge, endAge);
        return ResultMap.ok().data("users", users);
    }

    @ApiOperation("嵌套description描述查询人员信息")
    @GetMapping("/selectByDescription")
    public ResultMap selectByDescription(@RequestParam("keys") List<String> keys,
                                         @RequestParam("val") Object val){
        List<User> users = userService.selectByDescription(keys, val);
        return ResultMap.ok().data("users", users);
    }

    @ApiOperation("根据map嵌套查询人员信息——swagger测试不通，用ApiPost可以进行测试（swagger解析不了map）")
    @GetMapping("/selectByMap")
    public ResultMap selectByDescription(@RequestBody Map<String, Object> map){
        List<User> users = userService.selectByMap(map);
        return ResultMap.ok().data("users", users);
    }

}
